#!/bin/bash
version=$(date -d "8 hour" -u +%y.%m.%d.%H)
cd ~/app-root/data/temp/xpi/acvaa/
curl -s https://github.com/jiayiming/FireLocalSWF/commits/master |grep -o 'https://github.com/jiayiming/FireLocalSWF/commits/[a-z0-9]\{25,40\}'| sed -e '2,$d' -e 's#commits#commit#'>commits2.txt
if ! diff -q commits2.txt commits.txt; then
mv -f commits2.txt commits.txt
curl -s $(cat commits.txt) |egrep -o '/jiayiming/FireLocalSWF/blob/.*\.(swf|js)' | sed -e 's#blob#raw#' -e 's#^#https:\/\/github\.com&#' >list.txt
wget --no-check-certificate -N -i list.txt
if grep -q .swf list.txt; then
ls ~/app-root/data/temp/xpi/acvaa/ |grep -o "[a-zA-Z0-9_\.\-]*\.swf" >~/app-root/repo/swf/swf.txt
mv -f *.swf ~/app-root/data/swf/
List=$(ls ~/app-root/repo/swf/*.swf|grep -o "[a-zA-Z0-9_\.\-]*\.swf")
if [ "$List" ]; then
    echo '<!DOCTYPE HTML> <html><head><meta name="viewport" content="width=device-width, initial-scale=1"><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /></head><body><H2>当前位于 /</H2>'>~/app-root/repo/swf/index.html
    for i in $List; do
        echo "<A HREF=\"$i\">$i</A><br>">>~/app-root/repo/swf/index.html
        n=$(($n+1))
    done
    echo "<H3>共$n个文件</H3></body></html>">>~/app-root/repo/swf/index.html
fi
fi
if grep -q uc.js list.txt; then
mv -f *.uc.js YoukuAntiADs.uc.js
sed -i "/'re':/s#http:#https?:#" YoukuAntiADs.uc.js
cp -f YoukuAntiADs.uc.js YoukuAntiADs.txt
sed -i -e '/os: Cc/,$d' -e "/'player1': refD + 'o/d" -e "/'player0': refD + 'loader/s#player0#player#" -e "/'player0': refD + 'player/s#player0#player#" YoukuAntiADs.txt
sed -i -e "/player[0-9].*iqiyi_out.swf'/d" -e "/player[0-9].*iqiyi.swf'/d" -e "/player.*iqiyi5.swf'/s#player[0-9]*#player#" YoukuAntiADs.txt
cat YoukuAntiADs.txt ~/app-root/data/github-sh/xpi/acvaa/ext.txt >YoukuAntiADs2.uc.js
rm -f YoukuAntiADs.txt
cp -f YoukuAntiADs*.uc.js ~/app-root/runtime/repo/uc脚本/YoukuAntiADs/
sed -i "s#var refD.*#var refD = 'https://coding.net/u/HalfLife/p/swf/git/raw/gh-pages/';#" ~/app-root/runtime/repo/uc脚本/YoukuAntiADs/YoukuAntiADs*.uc.js
sed -i -e '1,/YoukuAntiADs.prototype/d' -e '/register observer/,$d' YoukuAntiADs*.uc.js
sed -i '1i\const Cc = Components.classes;\nconst Ci = Components.interfaces;\nconst Cr = Components.results;\nconst Cu = Components.utils\n\nvar EXPORTED_SYMBOLS = ["AntiChinaVideoAds"];\n\nCu.import("resource://gre/modules/NetUtil.jsm");\n\nfunction AntiChinaVideoAds() {};\nvar prefs = Cc["@mozilla.org/preferences-service;1"].getService(Ci.nsIPrefBranch);\nvar refD = prefs.getComplexValue("extensions.AntiChinaVideoAds.swfsource", Ci.nsISupportsString).data;\nAntiChinaVideoAds.prototype = {' YoukuAntiADs*.uc.js
sed -i 's#var xhr = new XMLHttpRequest();#var xhr = Cc["@mozilla.org/xmlextras/xmlhttprequest;1"].createInstance(Ci.nsIXMLHttpRequest);#' YoukuAntiADs.uc.js
cd ~/app-root/data/github-sh/xpi/acvaa/do/
sed -i -e "s#[0-9]\{2\}\.[0-9]\{2\}\.[0-9]\{2\}\.[0-9]\{2\}#${version}#" -e "/minggo/s#acvaa#acvaa2#" -e '/name/s#ACVAA\ #ACVAA2\ #' -e 's#acvaa.rdf#acvaa2.rdf#' install.rdf
mv -f ~/app-root/data/temp/xpi/acvaa/YoukuAntiADs.uc.js content/AntiChinaVideoAds.js
zip -r acvaa2.xpi *
rm -f ~/app-root/runtime/repo/扩展/$(ls ~/app-root/runtime/repo/扩展/|grep "acvaa2-[0-9]\{2\}\.[0-9]\{2\}\.[0-9]\{2\}\.[0-9]\{2\}.xpi")
mv -f acvaa2.xpi ~/app-root/repo/扩展/acvaa2-${version}.xpi
sed -n '/SITES\:/,/REFRULES: {/p' ~/app-root/data/temp/xpi/acvaa/YoukuAntiADs2.uc.js >../ext2.txt
sed -i -e 1d -e 's#refD#setting#' -e '$d' ../ext2.txt
sed -i '$s#,#;#' ../ext2.txt
cat ../ext1.txt ../ext2.txt ../ext3.txt >content/AntiChinaVideoAds.js
sed -i -e "s#[0-9]\{2\}\.[0-9]\{2\}\.[0-9]\{2\}\.[0-9]\{2\}#${version}#" -e "/minggo/s#acvaa2#acvaa#" -e '/name/s#ACVAA2\ #ACVAA\ #' -e 's#acvaa2.rdf#acvaa.rdf#' install.rdf
zip -r acvaa.xpi *
rm -f ~/app-root/data/temp/xpi/acvaa/YoukuAntiADs2.uc.js
rm -f ~/app-root/runtime/repo/扩展/$(ls ~/app-root/runtime/repo/扩展/|grep "acvaa-[0-9]\{2\}\.[0-9]\{2\}\.[0-9]\{2\}\.[0-9]\{2\}.xpi")
mv -f acvaa.xpi ~/app-root/repo/扩展/acvaa-${version}.xpi
~/app-root/data/github-sh/log/log.sh "acvaa[2]-${version}.xpi 更新"
rm -f ../ext2.txt
fi
else
rm -f commits2.txt
fi